package com.zm.zmgz.utils;

import com.zm.zmgz.dao.EmployeeBeanMapper;
import com.zm.zmgz.dao.UserBeanMapper;
import com.zm.zmgz.pojo.EmployeeBean;
import com.zm.zmgz.pojo.EmployeeBeanExample;
import com.zm.zmgz.pojo.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/04/07/11:48
 * @Description:通过sql及sql类型,获取"主键值/前主键值/新主键值"
 */
@Component
public class FindSqlPrimaryKeyUtils {

    @Autowired
    private UserBeanMapper userBeanMapper;

    @Autowired
    private EmployeeBeanMapper employeeBeanMapper;

    private static FindSqlPrimaryKeyUtils findSqlPrimaryKeyUtils;

    @PostConstruct
    public void init(){
        findSqlPrimaryKeyUtils = this;
        findSqlPrimaryKeyUtils.userBeanMapper = this.userBeanMapper;
        findSqlPrimaryKeyUtils.employeeBeanMapper = this.employeeBeanMapper;
    }

    /***
    * @Description: 获取员工表新增记录的用户名(身份证)
    * @Param: 
    * @return: 
    * @Author: cjw
    * @Date: 2020-04-07 11:55:45
    */
    public static String getInsertNewPrimaryKeyEmployeezm(String sql){
        String temp = sql;
        String[] split = sql.split(".+values \\(");
        String[] split1 = split[1].split(", ");
        String[] split2 = split1[2].split("\"");
        String name = split2[1];
        String[] split3 = split1[0].split("\"");
        String identificationNumber = split3[1];
        String parimaryKey = "新增员工" + name + "(" + identificationNumber + ")!";
        System.out.println(temp+"新主键:"+parimaryKey);
        return parimaryKey;
    }

    /***
     * @Description: 获取用户表新增记录的用户名(身份证)
     * insert into userzm (id, username, password, identificationNumber, kind, status ) values (null, "gaoyi", "123", "532128199508200000", null, 0 )
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-07 11:55:45
     */
    public static String getInsertNewPrimaryKeyUserzm(String sql){
        String temp = sql;
        String[] split4 = sql.split(", ");

        String s1 = split4[6];
        String[] split1 = s1.split("\"");
        String name = split1[1];

        String s2 = split4[8];
        String[] split2 = s2.split("\"");
        String identificationNumber = split2[1];

        String parimaryKey = "新增用户" + name+"("+identificationNumber+")!";
        System.out.println(temp+"新主键:"+parimaryKey);
        return parimaryKey;
    }

    /***
     * @Description: 获取员工表删除记录的用户名(身份证);通过sql获取身份证,继而获取员工名
     * delete from employeezm where identificationNumber = 440981199202161114
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-07 11:55:45
     */
    public static String getDeletOldPrimaryKeyEmployeezm(String sql){
        String temp = sql;
        String[] split4 = sql.split("delete from employeezm where identificationNumber = ");
        String identificationNumber = split4[1];
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        criteria.andIdentificationnumberEqualTo(identificationNumber);
        List<EmployeeBean> employeeBeanList = findSqlPrimaryKeyUtils.employeeBeanMapper.selectByExample(employeeBeanExample);
        if(!Objects.isNull(employeeBeanList) && employeeBeanList.size() > 0){
            EmployeeBean employeeBean = employeeBeanList.get(0);
            String parimaryKey = employeeBean.getName() + "(" + employeeBean.getIdentificationnumber() + ")员工信息被删除!";
            System.out.println(temp+"旧主键:" + parimaryKey);
            return parimaryKey;
        } else {
            return null;
        }

    }


    /***
     * @Description: 获取用户表删除记录的用户名(身份证);通过sql获取身份证,继而获取员工名
     * delete from userzm where id = 11
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-07 11:55:45
     */
    public static String getDeletOldPrimaryKeyUserzm(String sql){
        String temp = sql;
        String[] split4 = sql.split("delete from userzm where id = ");
        String id = split4[1];
        UserBean userBean = findSqlPrimaryKeyUtils.userBeanMapper.selectByPrimaryKey(Integer.parseInt(id));
        String parimaryKey = userBean.getUsername()+"("+userBean.getIdentificationnumber()+")用户被删除!";
        System.out.println(temp+"新主键:"+parimaryKey);
        return parimaryKey;
    }

    /***
     * @Description: 获取员工表更新记录的用户名(身份证);通过sql获取id,继而获取员工名和身份证
     * update employeezm set id = 1, name = "李艳美", picture = "null", employeeId = "a123", projectName = "北京追一", postName = "数据经理", referrer = "Vera", referrerPhone = "null", phone = "13552045683", email = "18730201380@163.com", trialSalary = 7.2, salary = 9, dateOfEntry = Fri Nov 08 00:00:00 CST 2019, dateOfDeparture = Thu Feb 13 00:00:00 CST 2020, sex = "女", old = 22, census = "河北邢台", cardNo = "6214830189018921", school = "河北软件技术职业学院", graduationDate = Sun Jul 01 00:00:00 CST 2018, highestEducation = "本科", major = "软件技术", marriage = "未婚未育", address = "北京市朝阳区平房镇姚家园东里八号院一号楼三单元", emergencyContact = "李海龙", emergencyContactRelation = "朋友", emergencyContactPhone = "15033511063", status = "0", remark = "1、工作认真负责，积极主动，能完全胜任本职工作，爱岗敬业，乐于助人，与同事相处融洽，善于合作。
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-07 11:55:45
     */
    public static String getUpdateOldPrimaryKeyEmployeezm(String sql){
        String temp = sql;
        String[] split4 = sql.split("update employeezm set id = ");
        String s = split4[1];
        String[] split = s.split(",");
        String id = split[0];
        EmployeeBeanExample employeeBeanExample = new EmployeeBeanExample();
        EmployeeBeanExample.Criteria criteria = employeeBeanExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(id));
        List<EmployeeBean> employeeBeanList = findSqlPrimaryKeyUtils.employeeBeanMapper.selectByExample(employeeBeanExample);
        EmployeeBean employeeBean = employeeBeanList.get(0);
        String parimaryKey = employeeBean.getName() + "(" + employeeBean.getIdentificationnumber() + ")员工信息被修改!";
        System.out.println(temp+"旧主键:" + parimaryKey);
        return parimaryKey;
    }

    /***
     * @Description: 获取员工表更新记录的用户名(身份证);通过sql获取id,继而获取员工名和身份证
     * update userzm set username = "gaoyi", password = "123", identificationNumber = "532128199508200000", kind = null, status = 1 where id = 12
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-04-07 11:55:45
     */
    public static String getUpdateOldPrimaryKeyUserzm(String sql){
        String temp = sql;
        String[] split4 = sql.split(".+where id = ");
        String id = split4[1];
        UserBean userBean = findSqlPrimaryKeyUtils.userBeanMapper.selectByPrimaryKey(Integer.parseInt(id));
        String parimaryKey = userBean.getUsername() + "(" + userBean.getIdentificationnumber() + ")用户信息被修改!";
        System.out.println(temp+"旧主键:" + parimaryKey);
        return parimaryKey;
    }


}
